package com.zeen.wima.common;

import com.zeen.wima.view.InitSetting;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class TraceService extends Service implements Runnable {

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		LocalData data = Wima.getInstance().getLocalData();
		if(!data.read("registed").equals("1"))
		{
			Intent setting = new Intent(this,InitSetting.class);
			setting.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(setting);
		}else{
			new Thread(this).start();
		}
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void run() {
		Intent startSelf = new Intent();
		startSelf.setClass(this, TraceService.class);
		PendingIntent pendingIntent = PendingIntent.getService(this, 0,
				startSelf, 0);
		AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		alarmManager.set(AlarmManager.RTC, getNextUpdateTime(), pendingIntent);
		Wima.getInstance().getTracer().trace();
	}

	public long getNextUpdateTime() {
		return System.currentTimeMillis() + 2 * 60 * 60 * 1000;
	}
}
